package com.example.user.handler;

import cn.hutool.json.JSONUtil;
import com.example.model.response.Result;
import com.example.redis.utils.CacheUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.example.model.constants.KeyConstants.KEY_NAME;
import static com.example.model.constants.KeyConstants.KEY_PERMISSION;

@Component
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    @Autowired
    private CacheUtil cacheUtil;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Boolean deletePerm = cacheUtil.delete(KEY_PERMISSION + request.getHeader("Authorization"));
        Boolean deleteName = cacheUtil.delete(KEY_NAME + request.getHeader("Authorization"));
        response.setStatus(200);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(JSONUtil.toJsonStr(Result.success("退出成功")));
    }
}
